<template>
  <el-dialog title="新增员工" :visible="showDialog" @close="btnCancel">
    <el-form
      ref="employData"
      label-width="80px"
      :model="employData"
      :rules="rules"
    >
      <el-form-item label="姓名" prop="username">
        <el-input v-model="employData.username" style="width: 400px" placeholder="请输入姓名" />
      </el-form-item>
      <el-form-item label="手机" prop="mobile">
        <el-input v-model="employData.mobile" style="width: 400px" placeholder="请输入手机号码" />
      </el-form-item>
      <el-form-item label="入职时间" prop="timeOfEntry">
        <el-date-picker v-model="employData.timeOfEntry" placeholder="选择入职时间" style="width: 400px" />
      </el-form-item>
      <el-form-item label="聘用形式" prop="formOfEmployment">

        <el-select v-model="employData.formOfEmployment" style="width: 400px" placeholder="请选择受聘用的形式">
          <el-option v-for="obj in EmployeeEnum.hireType" :key="obj.id" :label="obj.value" :value="obj.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="工号" prop="workNumber">
        <el-input v-model="employData.workNumber" style="width: 400px" placeholder="请输入入职时间" />
      </el-form-item>
      <el-form-item label="部门" prop="departmentName">
        <el-input
          v-model="employData.departmentName"
          style="width: 400px"
          placeholder="请输入部门"
          @focus="getDepartments"
        />
        <!-- default-expand-all是否默认展开 -->

        <el-tree
          v-if="isTreeShow"
          v-loading="loading"
          :data="treeData"
          :props="{label: 'name'}"
          :default-expand-all="true"
          @node-click="changeDepart"
        />
      </el-form-item>
      <el-form-item label="转正时间" prop="correctionTime">
        <el-date-picker v-model="employData.correctionTime" placeholder="选择转正时间" style="width: 400px" />
      </el-form-item>
    </el-form>

    <!-- <template v-slot:footer>
      <el-row type="flex" justify="center">
        <el-col :span="8">
          <el-button>取消</el-button>
          <el-button type="primary">确认</el-button>
        </el-col>
      </el-row>
    </template> -->
    <el-row slot="footer" type="flex" justify="center">
      <el-col :span="7">
        <el-button @click="btnCancel">取消</el-button>
        <el-button type="primary" @click="btnOK">确认</el-button>
      </el-col>
    </el-row>

  </el-dialog>
</template>

<script>
import { addEmployeeAPI } from '@/api'
import { getDepartmentsAPI } from '@/api'
// 转化数据为树形结构
import { transListToTreeData } from '@/utils'

// 引入数据 受聘状态
import EmployeeEnum from '@/api/constant/employees.js'
export default {
  props: {
    showDialog: {
      type: Boolean,
      default: true
    }
  },
  data() {
    return {
      // 接口提交数据
      employData: {
        username: '',
        mobile: '',
        formOfEmployment: '',
        workNumber: '',
        departmentName: '',
        timeOfEntry: '',
        correctionTime: ''
      },
      //  校验规则
      rules: {
        username: [
          { required: true, trigger: 'blur', message: '姓名不能为空' },
          {
            min: 1, max: 15, trigger: 'blur', message: '用户姓名为1-15位'
          }
        ],
        mobile: [
          { required: true, trigger: 'blur', message: '手机号不能为空' },
          {
            pattern: /^1[3-9]\d{9}$/, trigger: 'blur', message: '手机号要求1-11位，第二位是3-9'
          }
        ],
        formOfEmployment: [
          { required: true, trigger: 'blur', message: '聘用形式不能为空' }
        ],
        workNumber: [
          { required: true, trigger: 'blur', message: '工号不能为空' }
        ],
        departmentName: [
          { required: true, trigger: 'change', message: '部门不能为空' }
        ],
        timeOfEntry: [
          { required: true, trigger: 'blur', message: '入职时间不能为空' }
        ],
        correctionTime: [
          { required: true, trigger: 'blur', message: '转正时间不能为空' }
        ]
      },
      // 新增员工 => 部门数据
      treeData: [],
      // 部门 树是否显示
      isTreeShow: false,
      loading: false, // 加载
      // 枚举数据 受聘形式 正式/非正式
      EmployeeEnum: EmployeeEnum
    }
  },
  methods: {
    //   新增员工 => 点击部门 立马出席部门数据
    //   数据转化位树形结构
    async getDepartments() {
      // 加载状态开启
      this.loading = true
      const { depts } = await getDepartmentsAPI()
      this.treeData = transListToTreeData(depts, '')
      //   console.log(depts)
      this.isTreeShow = true
      this.loading = false
    },

    // 改变节点
    changeDepart(obj, node, v) {
    //   console.log(obj)
    //   console.log(node) // 点击的树节点
    //   console.log(v) // 节点组件本身
      this.employData.departmentName = obj.name
      this.isTreeShow = false
    },

    // 确认校验
    async btnOK() {
      try {
        await this.$refs.employData.validate()
        await addEmployeeAPI(this.employData)
        // 通知父组件重新获取员工信息
        this.$parent.getEmployeeMultiple()
      } catch (error) {
        console.log(error)
      }
    },
    btnCancel() {
      // 关闭弹层 sync大法
      this.$emit('update:showDialog', false)
      // 数据清除
      this.employData = {
        username: '',
        mobile: '',
        formOfEmployment: '',
        workNumber: '',
        departmentName: '',
        timeOfEntry: '',
        correctionTime: ''
      }
      // 校验清除
      this.$refs.employData.resetFields()
    }
  }

}
</script>

<style>

</style>
